package cn.tedu.charging.device.startup;

import cn.tedu.charging.device.dao.repository.StationRepository;
import cn.tedu.charging.device.pojo.vo.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Component
public class StationDataInit implements ApplicationRunner {

    @Autowired
    private StationRepository repository;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @PostConstruct
    public void init(){
        log.debug("PostConstruct init");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.debug("缓存预热,从数据库加载充电站信息开始...");
        log.debug("查询充电站数据......");
        List<StationPO> stationPOList= repository.listStation();
        log.debug("查询充电站数据成功:{}",stationPOList);

        HashMap<String, Point> points = new HashMap<>();
        BoundGeoOperations stations = redisTemplate.boundGeoOps("stations");

        HashOperations<String, Object, Object> redisHash = redisTemplate.opsForHash();
        for (StationPO stationPO : stationPOList) {
            //获取场站的经纬度
            BigDecimal stationLng = stationPO.getStationLng();
            BigDecimal stationLat = stationPO.getStationLat();

            String id = String.valueOf(stationPO.getId());
            Point point = new Point(Double.parseDouble(stationLng.toString()),
                    Double.parseDouble(stationLat.toString()));
            points.put(id,point);

            //2 把充电站的详情放入redis，用什么数据类型呢？ 是为了方便的通过id来查询

            //String key value                     Map<String,String>  可以  key：站的id value 站的详情
            //Set  map 套set set 放的是不重复的value  Map<String,Set<>>  key 站的id value set 放一个 站的id对应的站详情
            //Hash map 套map map 可以放重复 map结构        Map<String,Map<String,value>>
            //List map 套list List 可以放重复下标index   Map<String,List<>>
            //Zset map 套set set 放的是不重复的value并且可以排序 Map<String,TreeSet<>>

            //大Key
            String stationId = String.valueOf(stationPO.getId());
            HashMap<Object, Object> smallMap = new HashMap<>();
            //小Key和Value
            smallMap.put("name",stationPO.getStationName());
            smallMap.put("code",stationPO.getStationCode());
            smallMap.put("deviceNumber",stationPO.getDeviceNumber());
            smallMap.put("stationId",stationId);
            smallMap.put("operatorId",stationPO.getOperatorId());
            smallMap.put("Id",stationPO.getId());
            redisHash.putAll(stationId,smallMap);
        }
        stations.add(points);
    }
}
